package mornd.test2;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: mornd
 * @dateTime: 2023/5/6 - 16:28
 * 自定义切点解析匹配
 */
public class Demo2 {
    public static void main(String[] args) throws Exception {
        // 配置切点 springframework.aop 包下的 PointCut 接口实现类
        AspectJExpressionPointcut pc1 = new AspectJExpressionPointcut();
        // 设置切点表达式
        pc1.setExpression("execution(* foo())");
        boolean b1 = pc1.matches(I1.class.getMethod("foo"), Target1.class);
        boolean b2 = pc1.matches(I1.class.getMethod("bar"), Target1.class);
        System.out.println(b1 + "\t" + b2);

        AspectJExpressionPointcut pc2 = new AspectJExpressionPointcut();
        // 基于注解的切点表达式
        pc2.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");
        boolean b3 = pc2.matches(Target1.class.getMethod("foo"), Target1.class);
        boolean b4 = pc2.matches(Target1.class.getMethod("bar"), Target1.class);
        System.out.println(b3 + "\t" + b4);
    }

    interface I1 {
        void foo();

        void bar();
    }

    static class Target1 implements I1 {

        @Transactional
        @Override
        public void foo() {
            System.out.println("target1 foo");
        }

        @Override
        public void bar() {
            System.out.println("target1 bar");
        }
    }

    static class Target2 implements I1 {
        @Override
        public void foo() {
            System.out.println("target2 foo");
        }

        @Override
        public void bar() {
            System.out.println("target2 bar");
        }
    }
}
